package com.kkb.service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.mapper.TeamMapper;
import com.kkb.pojo.Team;
import com.kkb.pojo.TeamExample;
import com.kkb.vo.PageParam;
import com.kkb.vo.TeamVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * 球队 Service
 *
 * @author ztranscript 2021/8/30 16:14
 **/
@Service
public class TeamService {
    @Autowired
    private TeamMapper teamMapper;

    public PageInfo<Team> queryByPage(TeamVO param, PageParam pageParam) {
        TeamExample teamExample = buildTeamExample(param);

        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        List<Team> teamList = teamMapper.selectByExample(teamExample);
        return new PageInfo<>(teamList);
    }

    /**
     * 添加球队
     *
     * @param team 球队
     * @return 受影响的行数
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int addTeam(Team team) {
        return teamMapper.insertSelective(team);
    }

    /**
     * 根据主键删除球队
     *
     * @param teamId 球队主键
     * @return 受影响的行数
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int deleteTeam(Integer teamId) {
        Team team = teamMapper.selectByPrimaryKey(teamId);
        team.setIsDel(1);
        return teamMapper.updateByPrimaryKey(team);
    }

    /**
     * 批量删除球队信息
     *
     * @param teamIdList 待删除的 id 集合
     * @return 受影响的行数
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int batchDelete(List<Integer> teamIdList) {
        int rows = 0;
        for (Integer teamId : teamIdList) {
            rows += this.deleteTeam(teamId);
        }
        return rows;
    }


    /**
     * 根据主键查询
     *
     * @param teamId 球队 ID
     * @return 球队信息
     */
    public Team queryById(Integer teamId) {
        return teamMapper.selectByPrimaryKey(teamId);
    }

    /**
     * 根据主键更新
     *
     * @param team 球队信息
     * @return 受影响的行数
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int updateTeam(Team team) {
        return teamMapper.updateByPrimaryKeySelective(team);
    }

    /**
     * 根据查询字段构建 TeamExample
     *
     * @param param teamVO
     * @return teamExample
     */
    private TeamExample buildTeamExample(TeamVO param) {
        TeamExample teamExample = new TeamExample();
        TeamExample.Criteria criteria = teamExample.createCriteria();
        if (Objects.nonNull(param)) {
            String teamName = param.getTeamName();
            if (!StringUtils.isEmpty(teamName)) {
                criteria.andTeamNameLike("%" + teamName + "%");
            }
            String chineseName = param.getChineseName();
            if (!StringUtils.isEmpty(chineseName)) {
                criteria.andChineseNameLike("%" + chineseName + "%");
            }
            String coach = param.getCoach();
            if (!StringUtils.isEmpty(coach)) {
                criteria.andCoachLike("%" + coach + "%");
            }
            Integer area = param.getArea();
            if (Objects.nonNull(area) && !Objects.equals(area, -1)) {
                criteria.andAreaEqualTo(area);
            }

            Date beginDate = param.getBeginDate();
            Date endDate = param.getEndDate();
            if (Objects.nonNull(beginDate)) {
                criteria.andCreateTimeGreaterThanOrEqualTo(beginDate);
            }
            if (Objects.nonNull(endDate)) {
                criteria.andCreateTimeLessThanOrEqualTo(endDate);
            }
        }
        return teamExample;
    }
}
